<template>
  <el-dialog :append-to-body="true" :close-on-click-modal="false" :before-close="cancel" :visible.sync="dialog"
    :title="isAdd ? '新增' : '编辑'" width="800px">
    <el-form ref="form" :model="form" :rules="rules" size="small" label-width="140px">

      <el-form-item label="	出行方式名称" prop="modeName">
        <el-input v-model="form.modeName" style="width: 300px;" placeholder="请输入出行方式名称" />
      </el-form-item>
      <el-form-item label="出行方式描述" prop="description">
        <el-input v-model="form.description" style="width: 300px;" placeholder="请输入出行方式描述" />
      </el-form-item>
      <el-form-item label="白天起步价" prop="dayStartingPrice">
        <el-input-number min="0"	 v-model="form.dayStartingPrice" :min="0"></el-input-number>
      </el-form-item>
      <el-form-item label="	白天每公里费用" prop="dayCostPerKilometer">
        <el-input-number min="0"	 v-model="form.dayCostPerKilometer" :min="0"></el-input-number>
      </el-form-item>
      <el-form-item label="	晚上起步价" prop="eveningStartingPrice">
        <el-input-number min="0"	 v-model="form.eveningStartingPrice" :min="0"></el-input-number>
      </el-form-item>
      <el-form-item label="	晚上每公里费用" prop="eveningCostPerKilometer">
        <el-input-number min="0"	 v-model="form.eveningCostPerKilometer" :min="0"></el-input-number>
      </el-form-item>
      <el-form-item label="	覆盖距离（单位：公里）" prop="distanceCoverage">
        <el-input-number min="0"	 v-model="form.distanceCoverage" :min="0"></el-input-number>
      </el-form-item>
      <el-form-item label="	出行方式说明" prop="travelInstructions">
        <el-input v-model="form.travelInstructions" style="width: 300px;" placeholder="请输入出行方式描述" />
      </el-form-item>

      <el-form-item label="是否环保">
        <el-radio-group v-model="form.isEnvironmentFriendly" >
          <el-radio :label=1>环保</el-radio>
          <el-radio :label=0>不环保</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="是否可用">
        <el-radio-group v-model="form.isShow" >
          <el-radio :label=0>可用</el-radio>
          <el-radio :label=1>不可用</el-radio>
        </el-radio-group>
      </el-form-item>

    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="text" @click="cancel">取消</el-button>
      <el-button :loading="loading" type="primary" @click="doSubmit">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { addModes,editModes } from '@/api/technician'

export default {
  props: {
    isAdd: {
      type: Boolean,
      required: true
    }
  },

  data() {
    return {
      defaultStartDate: new Date(2023, 1, 1),
      loading: false, dialog: false,
      form: {
        description:'',//	出行方式描述
        distanceCoverage:0,//	覆盖距离（单位：公里）
        isEnvironmentFriendly:1,	//是否环保(0:不环保，1：环保）
        isShow:0,//	是否可用（0：是，1：否）
        modeName:'',//	出行方式名称
        dayCostPerKilometer:0,//白天每公里费用
        dayStartingPrice:0,//白天起步价
        eveningCostPerKilometer:0,//	晚上每公里费用
        eveningStartingPrice:0,//晚上起步价
        travelInstructions:'',//出行方式说明
      },
      CouponList: [],
      rules: {
        modeName: [
          { required: true, message: '请输入出行方式名称', trigger: 'blur' },
        ],

        dayCostPerKilometer: [
          { required: true, message: '请输入白天每公里费用', trigger: 'change' },
        ],
        dayStartingPrice: [
          { required: true, message: '请输入白天起步价', trigger: 'change' },
        ],
        eveningCostPerKilometer: [
          { required: true, message: '请输入晚上每公里费用', trigger: 'change' },
        ],
        eveningStartingPrice: [
          { required: true, message: '请输入晚上起步价', trigger: 'change' },
        ],
        travelInstructions: [
          { required: true, message: '请输入出行方式说明', trigger: 'change' },
        ],
        distanceCoverage: [
          { required: true, message: '请输入覆盖距离', trigger: 'change' },
        ],

      }
    }
  },
  methods: {

    getGoods(p) {
      var ids = []
      p.forEach((item, index) => {
        ids.push(item.id)
      })
      this.form.productId = ids.join(",")
    },
    couponsType(e) {
      this.form = {
        description:'',//	出行方式描述
        distanceCoverage:0,//	覆盖距离（单位：公里）
        isEnvironmentFriendly:1,	//是否环保(0:不环保，1：环保）
        isShow:0,//	是否可用（0：是，1：否）
        modeName:'',//	出行方式名称
        dayCostPerKilometer:0,//白天每公里费用
        dayStartingPrice:0,//白天起步价
        eveningCostPerKilometer:0,//	晚上每公里费用
        eveningStartingPrice:0,//晚上起步价
        travelInstructions:'',//出行方式说明
      }
    },
    cancel() {
      this.resetForm()
    },
    doSubmit() {
      if (this.isAdd) {
        this.doAdd()
      } else this.doEdit()
    },
    doAdd() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.loading = true
          addModes(this.form).then(res => {
            this.resetForm()
            this.$notify({
              title: '添加成功',
              type: 'success',
              duration: 2500
            })
            this.loading = false
            this.$parent.init()
          }).catch(err => {
            this.loading = false
            console.log(err.response.data.message)
          })
        }
      })
    },
    doEdit() {
      editModes(this.form).then(res => {
        this.resetForm()
        this.$notify({
          title: '修改成功',
          type: 'success',
          duration: 2500
        })
        this.loading = false
        this.$parent.init()
      }).catch(err => {
        this.loading = false
        console.log(err.response.data.message)
      })
    },
    resetForm() {
      this.dialog = false
      // this.$refs['form'].resetFields()
      this.form = {
        description:'',//	出行方式描述
        distanceCoverage:0,//	覆盖距离（单位：公里）
        isEnvironmentFriendly:1,	//是否环保(0:不环保，1：环保）
        isShow:0,//	是否可用（0：是，1：否）
        modeName:'',//	出行方式名称
        dayCostPerKilometer:0,//白天每公里费用
        dayStartingPrice:0,//白天起步价
        eveningCostPerKilometer:0,//	晚上每公里费用
        eveningStartingPrice:0,//晚上起步价
        travelInstructions:'',//出行方式说明
      }
    }
  }
}
</script>

<style scoped></style>
